package com.tyjc.unique;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;

import com.alibaba.fastjson.JSON;
import com.tyjc.unique.dialog.CustomProgress;
import com.tyjc.unique.net.StringResultBean;
import com.fyzk.activity.app.App;
import com.fyzk.activity.app.AppToast;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.Utils;

import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import http.XRequestCallBack;

/**
 * 所有activity的基类
 * 
 * @author shiqing 2015-6-12
 * 
 */
public abstract class _AnalaysesBaseFragment extends BaseFragment implements
		OnClickListener {

	public static String TAG = AnaylaseBaseActivity.class.getName();

//	protected HttpUtils httpUtils = new HttpUtils();
	private CustomProgress progressDg;
	public App mApp;
	protected Intent intent;
	protected Uri imgUri;
	protected Uri imgUri_addpic;

	/**
	 * get请求
	 * 
	 * @param requestCode
	 */
	public void httpGet(int requestCode, RequestParams params, boolean showProgress) {
		sendRequest(HttpMethod.GET, requestCode, params, showProgress);
	}

	/**
	 * post请求
	 * 
	 * @param requestCode
	 * @param params
	 * @param showProgress
	 *            是否显示加载提示
	 */
	public void httpPost(int requestCode, RequestParams params,
			boolean showProgress) {
		sendRequest(HttpMethod.POST, requestCode, params, showProgress);
	}

	public void sendRequest(HttpMethod method, int requestCode,
							RequestParams params, final boolean showProgress) {

		if (!Utils.isNetworkAvailable(getActivity())) {
			showToast("网络错误!");
			return;
		}

		if (showProgress) {
			showProgressDialog();
		}

		XRequestCallBack<String> callBack = new XRequestCallBack<String>(
				requestCode) {

			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if (showProgress) {
					cancelProgressDialog();
				}
				LogUtil.v("onSuccess联网返回："+result);
				try {
					StringResultBean resultBean = JSON.parseObject(result,
							StringResultBean.class);
					_AnalaysesBaseFragment.this.onSuccess(getRequestCode(), resultBean,
							result);
				} catch (Exception e) {
					e.printStackTrace();
					_AnalaysesBaseFragment.this.onFailure(getRequestCode());
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				super.onError(ex, isOnCallback);
				if (showProgress) {
					cancelProgressDialog();
				}
				LogUtil.v("onError联网返回："+ex.getMessage());
				_AnalaysesBaseFragment.this.onFailure(getRequestCode());
			}

			@Override
			public void onCancelled(CancelledException cex) {
				super.onCancelled(cex);
				if (showProgress) {
					cancelProgressDialog();
				}
				LogUtil.v("onSuccess联网返回："+cex.getMessage());
			}

			@Override
			public void onFinished() {
				super.onFinished();
				if (showProgress) {
					cancelProgressDialog();
				}
				LogUtil.v("onFinished联网返回：.......");
			}
		};
		LogUtil.v("联网请求："+params);
		if (method == HttpMethod.POST) {
			x.http().post(params, callBack);
		} else if (method == HttpMethod.GET) {
			x.http().get(params, callBack);
		} else {
			x.http().post(params, callBack);
		}




//		if (params == null) {
//			httpUtils.send(method, url, callBack);
//		} else {
//
//			String urlStr = "";
//			// 打印请求
//			if (params.getEntity() instanceof BodyParamsEntity) {
//				try {
//					urlStr = URLDecoder
//							.decode(url
//									+ "?"
//									+ Utils.InputStreamTOString(params
//											.getEntity().getContent()), "utf-8");
//					LogUtils.v("联网请求:"+urlStr);
//					if (method == HttpMethod.GET){
//						httpUtils.send(method, urlStr, callBack);
//						return;
//					}
//					List<NameValuePair> valueList = URLEncodedUtils
//							.parse(new URI(urlStr));
//					HashMap<String, String> requestMap = new HashMap<String, String>();
//					for (NameValuePair parameter : valueList) {
//						requestMap.put(parameter.getName(),
//								parameter.getValue());
//					}
//				} catch (IllegalStateException e) {
//					e.printStackTrace();
//				} catch (IOException e) {
//					e.printStackTrace();
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//
//			httpUtils.send(method, url, params, callBack);
//		}
	}

	/**
	 * 取消提示dialog显示
	 */
	public void cancelProgressDialog() {
		if (progressDg != null && progressDg.isShowing()) {
			progressDg.cancel();
		}
	}

	/**
	 * 显示提示
	 */
	public void showProgressDialog() {
		if (!getActivity().isFinishing()) {
			if (progressDg == null || !progressDg.isShowing()) {
				progressDg = CustomProgress.show(getActivity(), null, false, null);
			}
		}
	}

	/**
	 * 提示
	 * 
	 * @param text
	 */
	public void showToast(String text) {
		AppToast.getToast().show(text);
	}

	public void showToast(int resId) {
		AppToast.getToast().show(resId);
	}

	protected abstract void onSuccess(int requestCode,
									  StringResultBean resultBean, String result);

	protected abstract void onFailure(int requestCode);

	@Override
	public void onClick(View v) {

	}

	public Context getContext() {
		return getActivity();
	}

	/**
	 * 启动Activity
	 * 
	 * @param cls
	 *            void
	 */
	public void startActivity(Class<? extends Activity> cls) {
		Intent intent = new Intent(getActivity(), cls);
		startActivity(intent);
		// @SuppressWarnings("deprecation")
		// int version = Integer.valueOf(android.os.Build.VERSION.SDK);
		// if (version >= 5) {
		// this.overridePendingTransition(0, 0);
		// }
	}

}
